#include "datacollect_eeg2.h"
#include "ui_datacollect_eeg2.h"

#include <Utils/settingmanager.h>

DataCollect_EEG2::DataCollect_EEG2(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::DataCollect_EEG2)
{
    ui->setupUi(this);

    setupUI();
    setupSerialThread();
}



DataCollect_EEG2::~DataCollect_EEG2()
{
    // m_worker->stop();
    // delete hp_alpha;
    // delete hp_beta;
    delete hp_delta;
    // delete hp_gamma;
    // delete hp_theta;
    m_thread->quit();
    m_thread->wait();
    delete ui;
}
void DataCollect_EEG2::setupUI()
{
    customPlot_leftEEG = ui->widget_leftecg;
    customPlot_rightEEG = ui->widget_righteeg;
    customPlot_ir = ui->widget_ir;
    customPlot_red = ui->widget_red;

    scw_leftEEG=new SingCustomWave(customPlot_leftEEG);
    scw_rightEEG=new SingCustomWave(customPlot_rightEEG);
    scw_ir=new SingCustomWave(customPlot_ir);
    scw_red=new SingCustomWave(customPlot_red);

    hp_delta=new HotPlot(ui->widget_Map_Delta,100);
    hp_theta=new HotPlot(ui->widget_Map_Theta,100);
    hp_alpha=new HotPlot(ui->widget_Map_Alpha,100);
    hp_beta=new HotPlot(ui->widget_Map_Beta,100);
    hp_gamma=new HotPlot(ui->widget_Map_Gamma,100);


}
void DataCollect_EEG2::on_pb_close_clicked()
{
    m_worker->EEGStopOrder();//发送停止指令
    m_worker->ColseSerialPort();
    this->close();
}

void DataCollect_EEG2::handleEEGWaveData(QVector<double> leftEEGdata,QVector<double> rightEEGdata,QVector<double> irdata,QVector<double> reddata) {
    scw_leftEEG->SetData(leftEEGdata);
    scw_rightEEG->SetData(rightEEGdata);
    scw_ir->SetData(irdata);
    scw_red->SetData(reddata);

}
void DataCollect_EEG2::handleEEGRealIndexData(QVector<double> EEGIndexdata)
{
    ui->label_hr->setText(QString::number(EEGIndexdata.at(0),'f',0));
}
void DataCollect_EEG2::handleEEGEnergyIndexData(QVector<double>engeryIndex,QVector<double>f,QVector<double>psd)
{
    std::vector<double>deltav;
    qDebug()<<"engeryIndex[0]="<<engeryIndex.at(0);
    deltav.push_back(engeryIndex[0]);deltav.push_back(0);deltav.push_back(engeryIndex[0]);
    hp_delta->setData(deltav);
    hp_theta->setData(deltav);
    hp_alpha->setData(deltav);
    hp_beta->setData(deltav);
    hp_gamma->setData(deltav);

}
void DataCollect_EEG2::handleError(const QString &error)
{
    qDebug()<<error;
}

void DataCollect_EEG2::serialStartWork()
{
    qDebug()<<"串口已经成功打开";
}

void DataCollect_EEG2::on_pushButton_clicked()
{
    SettingManager SerialInfo("SerialInfo");
    QString portName = SerialInfo.getValue<QString>("spName",  "");
    // 启动串口发送开始指令
    m_worker->OpenSerialPort(portName, 460800);
    m_worker->EEGStartOrder();
}
void DataCollect_EEG2::setupSerialThread() {
    // 创建串口工作线程
    m_thread = new QThread(this);
    m_worker = new SerialWorker;//不能指定父对象，如果指定父对象不能放入m_thread线程里面了
    // 将worker移动到线程
    m_worker->moveToThread(m_thread);
    m_worker->MultiUSBDongleAPI_Init();
    // 连接信号槽
    connect(m_thread, &QThread::finished, m_worker, &QObject::deleteLater);
    connect(m_worker, &SerialWorker::newEEGWaveData, this, &DataCollect_EEG2::handleEEGWaveData);
    connect(m_worker, &SerialWorker::errorOccurred, this, &DataCollect_EEG2::handleError);
    connect(m_worker, &SerialWorker::OpenSeriaPortSuccess, this, &DataCollect_EEG2::serialStartWork);
    connect(m_worker, &SerialWorker::newEEGRealIndexData, this, &DataCollect_EEG2::handleEEGRealIndexData);
    connect(m_worker, &SerialWorker::newEEGEnergyIndexData, this, &DataCollect_EEG2::handleEEGEnergyIndexData);


    // 启动线程
    m_thread->start();
}


void DataCollect_EEG2::on_pushButton_2_clicked()
{
    m_worker->StartCompute();
}

